# -*- coding: utf-8 -*-
"""
    # @Author : xiaowei
    # @Time : 2022/7/7 14:51
    # @Software: PyCharm
"""
# 回合制对战游戏
# 角色，房间，战斗
import random
import time


class Role(object):
    def __init__(self, name, sex, blood=100):
        self.name = name
        self.sex = sex
        self.blood = blood
        self.room = None

    def attach(self, other):
        fight = {10: "普通攻击", 20: "物理攻击"}
        if self == other:
            print("不能攻击自己")
        else:
            if self.room and self.room == other.room:
                if self.blood <= 0:
                    print("您的血量低于0，不能进行攻击")
                else:
                    if random.choice([0, 1]) == 0:
                        lost_blood = random.choice([10, 20])
                        print(f"{other.name}没有装备")
                    else:
                        lost_blood = random.choice([10, 20])
                        print(f"{other.name}使用了装备")
                    other.blood = other.blood - lost_blood
                    print(f"{self.name}使用{fight[lost_blood]}攻击了{other.name}扣除了：{lost_blood}点血量，当前血量{other.blood}")
                    time.sleep(1)
                    if other.blood <= 0:
                        print(f"{other.name}已阵亡，不能再进行攻击")
                        exit()
            else:
                print("不在同一个房间不能攻击")


class Room(object):
    #	MAX = 2
    def __init__(self, name):
        self.name = name
        self.member_list = []

    def add_member(self, role):
        if role.room == None:
            if len(self.member_list) < 2:
                self.member_list.append(role)
                role.room = self
                print(f"{role.name}成功加入{self.name}房间")
        else:
            print(f"{role.name}已加入{role.room.name}房间")


roles = {}
rooms = {}
while True:
    print('''################
    1.创建角色
    2.创建房间
    3.加入房间
    4.开始战斗
    5.退出
################''')
    enter_number = input("请输入您的操作：")
    if enter_number.isdigit():
        enter_number = int(enter_number)
        if 1 <= enter_number <= 5:
            if enter_number == 1:
                name = input("请输入创建角色的姓名：")
                sex = input("请输入创建角色的性别：")
                roles[name] = Role(name, sex)
                print(f"{name}角色创建成功！")
            elif enter_number == 2:
                room = input("请输入创建房间的名字：")
                rooms[room] = Room(room)
                print(f"{room}房间创建成功！")
            elif enter_number == 3:
                print(f"当前角色有{roles.keys()}，当前房间有{rooms.keys()}")
                role_name = input("请选择您要加入的角色：")
                room_name = input("请选择您要加入的房间：")
                if role_name in roles.keys() and room_name in rooms.keys():
                    rooms[room_name].add_member(roles[role_name])
                else:
                    print("加入失败！你要加入的角色或房间不存在!")
            elif enter_number == 4:
                while True:
                    enter = random.choice([0, 1])
                    if enter == 0:
                        rooms[room_name].member_list[0].attach(rooms[room_name].member_list[1])
                    else:
                        rooms[room_name].member_list[1].attach(rooms[room_name].member_list[0])
            elif enter_number == 5:
                print("你已成功退出！")
                break
        else:
            print("输入错误！请输入1-5选择操作！")
    else:
        print("输入有误！请重新输入！")
